<?php
declare(strict_types=1);

namespace App\Console\Commands\Tasks;

use Illuminate\Console\Command;
use App\Repositories\LogRepository;

class JoinNobilityCommand extends Command
{
    //命令名
    protected $signature = 'task:joinNobility';

    //命令描述
    protected $description = '金币日志判断开通爵位';

    public function __construct()
    {
        parent::__construct();
    }

    //业务处理
    public function handle()
    {
        $repo_nobility = app('repo_nobility');
        $not_change_type = $repo_nobility->not_change_type;
        $recharge_change_type = $repo_nobility->recharge_change_type;
        $time = strtotime(date('Y-m-d H:i:00'));
        $between_date = [strtotime('-1 minute', $time), $time];

        $where = [
            ['user_money', '>', 0],
            ['change_time', '>', $between_date[0]],
            ['change_time', '<=', $between_date[1]],
        ];

        $account_log_list = app('model_account_log')
            ->whereNotIn('change_type', $not_change_type)
            ->whereHas('user_account_change_type', function ($query) {
                $query->where('symbol', 0);
            })
            ->where($where)
            ->selectRaw('id,user_id,change_time')
            ->get()->toArray();

        foreach ($account_log_list as $account_log) {
            $repo_nobility->joinNobility($account_log, 1);
        }

        $recharge_log_list = app('model_account_log')
            ->where($where)
            ->where(['change_type' => $recharge_change_type])
            ->selectRaw('id,user_id,change_time')
            ->get()->toArray();

        foreach ($recharge_log_list as $recharge_log) {
            $repo_nobility->joinNobility($recharge_log, 2);
        }
    }

}
